from lxml import etree

if __name__ == '__main__':
    root = etree.Element("root")

    child1 = etree.Element("child1")
    # 将 child1 作为 root 的子元素
    root.append(child1)
    print(etree.tostring(root, pretty_print=True).decode("utf-8"))

    parent = etree.Element("parent")
    # 将 child2  作为 root 的子元素标签名称
    etree.SubElement(parent, "child2")
    print(etree.tostring(parent, pretty_print=True).decode("utf-8"))
